﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/jquery-ui.min.js" />
///<reference path="../../scripts/kooboo/kooboo.form.js" />
///<reference path="order.create.customer.js" />
///<reference path="order.create.product.js" />





function GetAddress(name) {
    var customer = GetCustomer();
    if (customer != null) {
        for (var i = 0; i < customer.AddressBooks.length; i++) {
            var item = customer.AddressBooks[i];
            if (item.Id == name) {
                return item;
            }
        }
    }

    return null;
}

function GetCustomer() {
    var customerid = $("#customer-id").val();
    for (var i = 0; i < Customers.length; i++) {
        var item = Customers[i];
        if (item.Id == customerid) {
            return item;
        }
    }

    return null;
}

$(function () {
    $("#same-address").click(function (e) {
        var shippingAddress = $("#shipping-address");
        var billingAddress = $("#billing-address");

        if ($(this).attr("checked") === true) {
            billingAddress.find("input[name=InvoiceFirstName]").val(shippingAddress.find("input[name=FirstName]").val());
            billingAddress.find("input[name=InvoiceLastName]").val(shippingAddress.find("input[name=LastName]").val());
            billingAddress.find("input[name=InvoiceAddress1]").val(shippingAddress.find("input[name=Address1]").val());
            billingAddress.find("input[name=InvoiceAddress2]").val(shippingAddress.find("input[name=Address2]").val());
            billingAddress.find("select[name=InvoiceCountry.CountryCode]").val(shippingAddress.find("select[name=Country.CountryCode]").val());
            billingAddress.find("input[name=InvoiceState]").val(shippingAddress.find("input[name=State]").val());
            billingAddress.find("input[name=InvoiceCity]").val(shippingAddress.find("input[name=City]").val());
            billingAddress.find("input[name=InvoicePostcode]").val(shippingAddress.find("input[name=Postcode]").val());
            billingAddress.find("input[name=InvoicePhone]").val(shippingAddress.find("input[name=Phone]").val());
        }
    });


    $("#shipping-address").find(".addressbook-selector").change(function (e) {
        var address = GetAddress($(this).val());
        if (address != null) {
            var shippingAddress = $("#shipping-address");
            shippingAddress.find("input[name=FirstName]").val(address.FirstName);
            shippingAddress.find("input[name=LastName]").val(address.LastName);
            shippingAddress.find("input[name=Address1]").val(address.Address1);
            shippingAddress.find("input[name=Address2]").val(address.Address2);
            shippingAddress.find("select[name=Country.CountryCode]").val(address.CountryCode);
            shippingAddress.find("input[name=State]").val(address.State);
            shippingAddress.find("input[name=City]").val(address.City);
            shippingAddress.find("input[name=Postcode]").val(address.Postcode);
            shippingAddress.find("input[name=Phone]").val(address.Phone);
        }

    });

    $("#billing-address").find(".addressbook-selector").change(function (e) {
        var address = GetAddress($(this).val());
        if (address != null) {
            var billingAddress = $("#billing-address");
            billingAddress.find("input[name=InvoiceFirstName]").val(address.FirstName);
            billingAddress.find("input[name=InvoiceLastName]").val(address.LastName);
            billingAddress.find("input[name=InvoiceAddress1]").val(address.Address1);
            billingAddress.find("input[name=InvoiceAddress2]").val(address.Address2);
            billingAddress.find("select[name=InvoiceCountry.CountryCode]").val(address.CountryCode);
            billingAddress.find("input[name=InvoiceState]").val(address.State);
            billingAddress.find("input[name=InvoiceCity]").val(address.City);
            billingAddress.find("input[name=InvoicePostcode]").val(address.Postcode);
            billingAddress.find("input[name=InvoicePhone]").val(address.Phone);
        }
    });
});








$(function () {
    //Step link
    $('.step-link').click(function () {
        var disabled = $(this).attr("disabled");
        if (disabled) {
            alert($(this).attr("message"));
            return;
        }
        target = $(this).attr("href");
        step = target.replace("#", "step-");
        $('.step-content').addClass("hide");
        $(target).removeClass("hide");
        $('.step li.current').removeClass("current");
        $('.step').find('li.' + step).addClass("current");
    });

})

function EnableCalculator() {
    $(".calculate").show();
}

$(function () {
    $("#shipping-address").click(function (e) {
        EnableCalculator();
    });

    $("#billing-address").click(function (e) {
        EnableCalculator();
    });

    $("#PaymentName").change(function (e) {
        EnableCalculator();
    });

    $("#ShippingId").change(function (e) {
        EnableCalculator();
    });

    $(".calculate").click(function (e) {
        e.preventDefault();

        $(this).ajaxPost({
            success: function (data) {
                $("#SubTotal").html(data.SubTotal);
                $("#ShippingAndHandling").html(data.ShippingAndHandling);
                $("#GrandTotal").html(data.GrandTotal);
                $("#TotalPaid").html(data.TotalPaid);
                $("#TotalRefunded").html(data.TotalRefunded);
                $("#TotalDue").html(data.TotalDue);

                $(".calculate").hide();
            }
        }, $("#create-form").serialize());
        
        return false;
    });



    $("#create-form").submit(function (e) {
        e.preventDefault();
        $(this).ajaxPost({
            success: function () {
                window.location.href = $("#back-to-list").attr("href");
            }
        });
        return false;
    });
});

